--- title: "±2σ" author: ~ date: "2020-03-20 22:27:01" slug: 'minos_issue' categories: [] tags: [] Categories: - 資産運用 Description: '' Tags: - 資産運用 menu: '' output: html_document: toc: true toc_float: true code_folding: show md_extensions: -ascii_identifiers ---

+-2sigma.Rmdのロジックを良くしたい
やりたいこと
mm/ddのリターンが過去〇〇日のリターンの-2σ以上なのか以下なのか判別したい

前処理

-2σ計算

# rm(tmp)
# rm(tmp1)
# rm(tmp2)
# rm(touraku2)


#過去1週間分を評価する
  #結果テーブル準備
    # tmp2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
    #                        'sigma'=0.99 , 'buysign'='buy')[-1,]
    # tmp2 <- as.Date(tmp2[,1])
    # touraku2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
    #                        'sigma'=0.99 , 'buysign'='buy')[-1,]
    # touraku2[,1] <- as.Date(touraku2[,1])
    tmp2 <- dailyrtn
    tmp2 <- transform(tmp2, sigma=0)
    tmp2 <- transform(tmp2, buysign='--')
    tmp2[,1] <- ymd(tmp2[,1])
    tmp2[,6] <- as.character(tmp2[,6])
    tmp2 <- tmp2[-1,]
    touraku2 <- head(dailyrtn,1)
    touraku2 <- transform(touraku2, sigma=0)
    touraku2 <- transform(touraku2, buysign='--')
    touraku2[,1] <- ymd(touraku2[,1])
    touraku2[,6] <- as.character(touraku2[,6])
    touraku2 <- touraku2[-1,]

  for (i in 1:length(ticker_tmp)){
    
    tmp <- subset(dailyrtn, ticker == ticker_tmp[i])
    #列追加
      tmp <- transform(tmp, sigma=0)
      tmp <- transform(tmp, buysign='--')
      tmp$date <- ymd(tmp$date)
      tmp[,6] <- as.character(tmp[,6])
      tmp <- tmp[-1,]

    #1週間前の位置 1週間前から現在までのcloseを評価するの意
      tarRow1 <- nrow(tmp) - 6
      
      for (k in tarRow1:nrow(tmp)) {
      #過去300日分のリターンの-2σ計算
        #tarRow2 <- k - 300
        #過去300日分の抽出
          #tmp1 <- subset(tmp, 
          #               as.numeric(row.names(tmp)) >= tarRow2
          #               & row.names(tmp) <= tarRow1 )
          tmp1 <- tmp[1:tarRow1,]
          tmp1 <- tail(tmp1, 300)
        #-2σ計算
          under2sigma <- quantile(tmp1$rtn,c(0.02275))
        #-2σの追記
          tmp[k, 5] <- under2sigma
        #リターンとunder2sigmaを比較してbuyサインを立てる
          if (tmp[k, 4] < tmp[k, 5]) {
            tmp[k, 6] <- 'buy'
          } else {
            tmp[k, 6] <- '-'
          }
      }
    #結果をテーブルへ
      tmp2 <- rbind(tmp2, tmp)
      touraku2 <- subset(tmp2, buysign=='buy')
      touraku2$ticker <- as.factor(touraku2$ticker)
      #1週間以内でフィルタ
        touraku2 <- touraku2 %>%
          filter(date >= Sys.Date()-7)
  }
  datatable(touraku2,
          filter = 'top', 
          style = 'bootstrap', class = 'table-bordered table-condensed',
          extensions = 'ColReorder',
          options = list(dom = 'Rlfrtip')
          )
  #write.table(touraku2,'touraku2.txt',sep="\t",quote=F,row.names = F)
  
  ticker_bakusagari <- as.character(t(touraku2$ticker))

#購入済で下がった銘柄は?
  ticker_kaimashikoho <- intersect(ticker_bakusagari, kounyu)
#購入済でない銘柄は?
  #setdiff(a,b)はaにあってbにないものを抽出する
  ticker_shinkikoho <- setdiff(ticker_bakusagari, kounyu)
  
#  tmp <- subset(kokunaiETF, kokunaiETF$ticker %in% ticker_bakusagari)
#  datatable(tmp,
#        filter = 'top', 
#        style = 'bootstrap', class = 'table-bordered table-condensed',
#        extensions = 'ColReorder',
#        options = list(dom = 'Rlfrtip')
#        )

購入済で下がった銘柄
2510

新規購入候補の銘柄
1305, 1306, 1308, 1348, 1473, 1475, 2557, 1320, 1321, 1330, 1329, 1346, 1578, 1369, 1397, 2525, 1591, 1592, 1593, 1599, 1364, 1474, 1319, 2516, 1563, 1551, 1311, 1344, 1312, 1492, 1493, 1618, 1619, 1620, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629, 1630, 1631, 1632, 1633, 1615, 1670, 1698, 1577, 1585, 1586, 2523, 1477, 1478, 1399, 1479, 1480, 1481, 1484, 1489, 1494, 1651, 1652, 2518, 1653, 1498, 2529, 2560, 1490, 1499, 1568, 1367, 1569, 1457, 1356, 1368, 1572, 1570, 1579, 1358, 1365, 1458, 1464, 1467, 1470, 1465, 1466, 1469, 1472, 1343, 1345, 1597, 1595, 1398, 1476, 1488, 2552, 2555, 2556, 2517, 2527, 2528, 1555, 1495, 1659, 1660, 2515, 1313, 1584, 1322, 2530, 1678, 1559, 1560, 1390, 1385, 1386, 1387, 1388, 1389, 1391, 1392, 1679, 1546, 1547, 2521, 1545, 1323, 1324, 1325, 1394, 1550, 2513, 2514, 1681, 2520, 1554, 1657, 1658, 2522, 1349, 1677, 2511, 2512, 1566, 2519, 1482, 1486, 1487, 1496, 1497, 1656, 2554, 1328, 1326, 1672, 1540, 1683, 1541, 1682, 1673, 1542, 1543, 1671, 1699, 1690, 1696, 2031, 2033, 2035, 2036, 2037, 2038, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2048, 2050, 2066, 2067, 2068, 2069, 2979, 2972, 2971, 3493, 3492, 3488, 3487, 3481, 3478, 3476, 3473, 3472, 3471, 3470, 3468, 3466, 3463, 3462, 3459, 3455, 3453, 3309, 3451, 3308, 3298, 3296, 3295, 3292, 3290, 3287, 3283, 3282, 3281, 3279, 3278, 8979, 3269, 3249, 3234, 3227, 3226, 8963, 8987, 8986, 8985, 8984, 8977, 8976, 8975, 8972, 8968, 8967, 8966, 8964, 8961, 8960, 8958, 8957, 8956, 8955, 8954, 8953, 8952, 8951

グラフ(確認用)

#国内

if (length(intersect(touraku2$ticker, kokunaiETF)) >= 1) {
  g <- ggplot(NULL)
  g <- g + geom_line(data = subset(daily1,
                                   ticker %in% intersect(touraku2$ticker, kokunaiETF) &
                                   year(date) >= 2017),
                     aes(x=date, y=close, colour=ticker))
  g <- g + geom_point(data = subset(touraku2,
                                    ticker%in% intersect(touraku2$ticker, kokunaiETF) &
                                    year(date) >= 2017),
                      aes(x=date, y=close, colour=ticker))
  ggplotly(g)
} else {
  tmp <- 0
}

#海外
if (length(intersect(touraku2$ticker, kaigai)) >= 1) {
  g <- ggplot(NULL)
  g <- g + geom_line(data = subset(daily1,
                                   ticker %in% intersect(touraku2$ticker, kaigai) &
                                   year(date) >= 2017),
                     aes(x=date, y=close, colour=ticker))
  g <- g + geom_point(data = subset(touraku2,
                                    ticker%in% intersect(touraku2$ticker, kaigai) &
                                    year(date) >= 2017),
                      aes(x=date, y=close, colour=ticker))
  ggplotly(g)
} else {
  tmp <- 0
}

  ggplotly(
      ggplot(subset(monthlyrtn, ticker %in% ticker_bakusagari),
             aes(x = return_monthly, colour = ticker)
             ) + geom_density()
  )

# 購入済銘柄との相関確認

# ticker4 <- c()
# f02 <- daily1 %>%
#   filter(ticker %in% ticker_bakusagari | ticker %in% kounyu,
#          date >= Sys.Date() - 365)
# f02 <- dcast(f02, date ~ ticker, value.var='close')
# f02 <- f02[,2:ncol(f02)]
# f02 <- f02[,order(factor(colnames(f02),levels=ticker4))]
# cor.plot(cor(f02, use='pairwise.complete.obs', method='p'),
#          numbers=T)
# corrplot.mixed(cor(f02, use='pairwise.complete.obs', method='p'),
#                order = "hclust", tl.col = "black")

#qgraph
  # f03 <- f02
  # groups <- list("購入済"=1:length(kounyu),"新規"=length(kounyu)+1:length(ticker_shinkikoho))
  # qgraph(cor(f03, use='pairwise.complete.obs', method='p'),groups=groups)
    $(function(){
      $("img:not(.lb-image)").wrap(function() {
        return "<a href='" + $(this).attr("src") + "' data-lightbox='" + $(this).attr("scr") + "'></a>";
      });
    });